# -*- coding: utf-8 -*-
# @Time    : 2021/4/3 14:32
# @Author  : Zhangrenjie
# @Email   : zhangrenjie1998@163.com
# @File    : Walden_count.py
# @Software: PyCharm

import string
import copy

def count_word(filename):
    with open(filename, "r", encoding='utf-8') as f:
        word_list = f.read().translate(str.maketrans('', '', string.punctuation)).lower().split()
        print(word_list)
        result = dict()
        for word in word_list:
            if word in result:
                result[word] += 1
            else:
                result[word] = 1
    return sorted(result.items(), key=lambda x: (x[1], x[0]), reverse=True)


# def count_word(filename):
#     with open(filename, "r", encoding='utf-8') as f:
#         file1 = f.read()
#         file2 = copy.deepcopy(file1)
#         for i in file1:
#            if i in string.punctuation:
#                 file2 = file2.replace(i, '')
#         word_list = file2.lower().split()
#         print(word_list)
#         result = dict()
#         for word in word_list:
#             if word in result:
#                 result[word] += 1
#             else:
#                 result[word] = 1
#     return sorted(result.items(), key=lambda x: (x[1], x[0]), reverse=True)
#
result_dict = count_word("Walden.txt")

for i in range(10):
    print(f"第{i + 1}名是：{result_dict[i][0]},出现{result_dict[i][1]}次")


